package com.omega.framework.common.util.date;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
 * 时间校验器
 *
 * @author Lv.
 * @date 2022/11/2 12:07
 */
public class DateValidatorUsingDateFormat implements DateValidator {
    private final DateTimeFormatter dateFormatter;

    public DateValidatorUsingDateFormat(DateTimeFormatter dateFormatter) {
        this.dateFormatter = dateFormatter;
    }


    @Override
    public boolean isValid(String dateStr) {
        try {
            LocalDate.parse(dateStr, this.dateFormatter);
        } catch (DateTimeParseException e) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        final DateValidatorUsingDateFormat dateValidatorUsingDateFormat = new DateValidatorUsingDateFormat(DateTimeFormatter.ofPattern(DateUtil.TIME_FORMAT_DIGITAL));
        System.out.println(dateValidatorUsingDateFormat.isValid("2020-11-11"));
        System.out.println(dateValidatorUsingDateFormat.isValid("2020-11-11 00:00:00"));
    }

}
